package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.myapplication.R;
import com.example.myapplication.activity.VideoPlayActivity;
import com.example.myapplication.entity.RecommendVideo;

import java.util.ArrayList;
import java.util.List;

public class VideoRecommendAdapter extends RecyclerView.Adapter<VideoRecommendAdapter.VideoRecommendHolder>{
    private Context context;
    private VideoRecommendItemAdapter adapter;
    private List<RecommendVideo.DatasBean> datasBeanList;
    private int count = 0;

    public VideoRecommendAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<RecommendVideo.DatasBean> datasBeanList){
        this.datasBeanList = datasBeanList;
        this.count++;
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public VideoRecommendHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_video_recommend,parent,false);
        return new VideoRecommendHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoRecommendHolder holder, int position) {
        if(position==count-1&&datasBeanList!=null&&datasBeanList.size()!=0) {
            RecommendVideo.DatasBean datasBean = datasBeanList.get(0);
            holder.card_title.setText(datasBean.getData().getTitle());
            holder.card_text.setText(datasBean.getData().getCreator().getNickname());
            Glide.with(context).load(datasBean.getData().getCoverUrl())
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .error(R.drawable.ic_baseline_refresh_24)
                    .placeholder(R.drawable.ic_baseline_refresh_24)
                    .into(holder.card_img);
            loadData();
        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    class VideoRecommendHolder extends RecyclerView.ViewHolder {
        TextView card_title;
        CardView cardView;
        TextView card_text;
        ImageView card_img;
        RecyclerView recycler_view_item_video;
        TextView refresh;
        TextView tips;
        ScrollView scrollView;
        public VideoRecommendHolder(@NonNull View itemView) {
            super(itemView);
            scrollView = itemView.findViewById(R.id.scrollView);
            refresh = itemView.findViewById(R.id.refresh);
            tips = itemView.findViewById(R.id.tips);
            card_title = itemView.findViewById(R.id.card_title);
            cardView = itemView.findViewById(R.id.cardView);
            card_text = itemView.findViewById(R.id.card_text);
            card_img = itemView.findViewById(R.id.card_img);
            recycler_view_item_video = itemView.findViewById(R.id.recycler_view_item_video);
            RecyclerView.LayoutManager manager = new GridLayoutManager(context,2);
            recycler_view_item_video.setLayoutManager(manager);
            adapter = new VideoRecommendItemAdapter(context);
            recycler_view_item_video.setAdapter(adapter);

            cardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, VideoPlayActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("video",datasBeanList.get(0));
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });
        }
    }
    private void loadData(){
        List<RecommendVideo.DatasBean> list = new ArrayList<>();
        for(int i=1;i<datasBeanList.size()-1;i++){
            list.add(datasBeanList.get(i));
        }
        adapter.setData(list);
    }

}
